home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / misc / splines.lha / Splines / sprt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-16  |  8.0 KB  |  285 lines

  1. /* The routines in this file are copyright (c) 1987 by Helene (Lee) Taran.
  2.  * Permission is granted for use and free distribution as long as the
  3.  * original author's name is included with the code.
  4.  */
  5.  
  6. /*
  7.  * Reformatted, and modified to compile without warnings and errors
  8.  * under SAS/C -6.5x by gduncan@philips.oz.au (GMD). This included
  9.  * proto generation, renaming of some literals to avoid name collisions,
  10.  * and Amiga Version string (also displayed in Title bar).
  11.  * No original version number, this version arbitrarily named 1.1. 
  12.  * - otherwise no functional changes.
  13.  * GMD - Sep 94
  14.  */
  15.  
  16. #include "all.h"
  17.  
  18. INTUITIONBASE *IntuitionBase;
  19. GFXBASE *GfxBase;
  20.  
  21. LIBRARY *LayersBase;        /*GMD */
  22.  
  23. USHORT pointimage[] =        /* what a control point will look like */
  24. {0x0000, 0x0000, 0x1c00, 0x3e00, 0x3e00, 0x3e00, 0x1c00};
  25.  
  26. IMAGE control_image =
  27. {-4, -4, 7, 7, 1, &pointimage[0], 0x1, 0x0, NULL};
  28.  
  29. USHORT ColorTable[COLORCOUNT] =    /* Here are the actual colors used      */
  30. {
  31.   0x0000,            /* black */
  32.   0x0063,            /* greenish */
  33.   0x0f24,            /* redish  */
  34.   0x000f,            /* blue */
  35. };
  36.  
  37. NEWWINDOW WindowInfo =
  38. {WINDOW_LEFT,            /* Left Edge                               */
  39.  WINDOW_TOP,            /* Top Edge                                */
  40.  WINDOW_WIDTH,            /* Initial Width       (640 =Hires)        */
  41.  WINDOW_HEIGHT,            /* Initial Height      (400 = interlaced)  */
  42.  (UBYTE) - 1,            /* DetailPen   (-1 = use screen's)         */
  43.  (UBYTE) - 1,            /* BlockPen    (-1 = use screen's)         */
  44.  MOUSEBUTTONS | CLOSEWINDOW,    /* <-- IDCMP flags; \/ window flags     */
  45.  SMART_REFRESH | WINDOWCLOSE | ACTIVATE | REPORTMOUSE,
  46.  NULL,                /* First Gadget                    */
  47.  NULL,                /* CheckMark    (used with menus)  */
  48.  (UBYTE *) "Splines",        /* Title                           */
  49.  NULL,                /* Screen       (null = default)   */
  50.  NULL,                /* BitMap                          */
  51.  1,                /* MinWidth   (0 = initial value)  */
  52.  1,                /* MinHeight  (0 = initial value)  */
  53.  0,                /* MaxWidth   (0 = initial value)  */
  54.  0,                /* MaxHeight  (0 = initial value)  */
  55.  CUSTOMSCREEN,            /* Type                            */
  56. };
  57.  
  58. /*
  59.  * Following string (referenced in ScreenInfo) has Version string
  60.  * appended in main() - GMD
  61.  */
  62.  
  63. char TitleBar[80] = "Splines and PopUp Menus:   by Lee Taran -  ";
  64.  
  65. /* The following structure gives all the information that Intuition
  66.  * needs to maintain a customized screen.
  67.  */
  68.  
  69. NEWSCREEN ScreenInfo =
  70. {
  71.   0, 0,                /* Left Edge, Top Edge   */
  72.   SCREEN_WIDTH, SCREEN_HEIGHT,    /* Screen Width & Height */
  73.   DEPTH,            /* Number of bit-planes  */
  74.   DETAILPEN,            /* Detail Pen color register */
  75.   BLOCKPEN,            /* Background color register */
  76.   VIEWMODE,            /* ViewMode */
  77.   CUSTOMSCREEN,            /* Type */
  78.   NULL,                /* Font : null = use default */
  79.   TitleBar,            /* Screen Title */
  80.   NULL,                /* Gadgets */
  81.   NULL                /* CustomBitMap */
  82. };
  83.  
  84. /* Bits to keep track of anything that must eventually be deallocated */
  85. int OpenBits = 0x0000;
  86.  
  87. /* Intuition structures that will be used to setup the background
  88.  * environment. 
  89.  */
  90. WINDOW *Window;
  91. SCREEN *Screen;
  92. LAYER_INFO *Layer_Info;
  93.  
  94. SPL_POPUP_MENU PointMenu, CurveMenu;
  95. SPL_POPUP_ITEM PointItem[] =
  96. {
  97.   {"Add Point After", ADD_AFTER, 1, 1, 0, 0, DETAILPEN, NULL},
  98.   {"Add Point Before", ADD_BEFORE, 1, 2, 0, 0, DETAILPEN, NULL},
  99.   {"Move This Point", MOVE_POINT, 1, 2, 0, 0, DETAILPEN, NULL},
  100.   {"Remove This Point", REMOVE_POINT, 1, 2, 0, 0, DETAILPEN, NULL}
  101. };
  102.  
  103. SPL_POPUP_ITEM CurveItem[] =
  104. {
  105.   {"Open B-Spline Natural Ends", OPENB_NATURAL, 1, 2, 0, 0, DETAILPEN, NULL},
  106.   {"Open B-Spline Triple Knots", OPENB_TRIPLE, 1, 2, 0, 0, DETAILPEN, NULL},
  107.   {"Open Interpolating Spline", OPEN_INTRPL, 1, 2, 0, 0, DETAILPEN, NULL},
  108.   {"Closed B-Spline", CLOSEDB, 1, 2, 0, 0, DETAILPEN, NULL},
  109. {"Closed Interpolating Spline", CLOSED_INTRPL, 1, 2, 0, 0, DETAILPEN, NULL},
  110.   {"Toggle AFrame", TOGGLEAFRAME, 1, 2, 0, 0, DETAILPEN, NULL},
  111.   {"Redraw", REDRAW, 1, 2, 0, 0, DETAILPEN, NULL},
  112.   {"  GET ME OUT OF HERE!!", QUIT, 1, 5, 0, 0, OUTLINEPEN, NULL},
  113. };
  114.  
  115. void
  116. Init_PointMenu ()
  117. {
  118.   PointItem[0].next = &PointItem[1];
  119.   PointItem[1].next = &PointItem[2];
  120.   PointItem[2].next = &PointItem[3];
  121.  
  122.   PointMenu.depth = 2;
  123.   PointMenu.width = 100;    /* minimum width */
  124.   PointMenu.deactivate = SELECTUP;
  125.   PointMenu.outline_color = OUTLINEPEN;
  126.   PointMenu.area_color = BLOCKPEN;
  127.   PointMenu.first_item = &PointItem[0];
  128.   if (!Init_PopUp_Menu (&PointMenu))
  129.     panic (CASE_POPUP_MENU);
  130. }
  131.  
  132. void
  133. Init_CurveMenu ()
  134. {
  135.   CurveItem[0].next = &CurveItem[1];
  136.   CurveItem[1].next = &CurveItem[2];
  137.   CurveItem[2].next = &CurveItem[3];
  138.   CurveItem[3].next = &CurveItem[4];
  139.   CurveItem[4].next = &CurveItem[5];
  140.   CurveItem[5].next = &CurveItem[6];
  141.   CurveItem[6].next = &CurveItem[7];
  142.  
  143.   CurveMenu.depth = 2;
  144.   CurveMenu.width = 100;    /* minimum width */
  145.   CurveMenu.deactivate = SELECTUP;
  146.   CurveMenu.outline_color = OUTLINEPEN;
  147.   CurveMenu.area_color = BLOCKPEN;
  148.   CurveMenu.first_item = &CurveItem[0];
  149.   if (!Init_PopUp_Menu (&CurveMenu))
  150.     panic (CASE_POPUP_MENU);
  151.  
  152. }
  153.  
  154. void
  155. OpenLibraries ()
  156. {
  157.   /* Open the Intuition library -- connect to the routines in ROM 
  158.    * If the result is NULL then something went wrong so exit immediately
  159.    */
  160.   if (!(IntuitionBase = (INTUITIONBASE *)
  161.     OpenLibrary ("intuition.library", INTUITION_VERSION)))
  162.     panic (CASE_INTUITION);
  163.   else
  164.     OpenBits |= CASE_INTUITION;
  165.  
  166.   /* Open the Graphics Library -- so that we can use the bit-map
  167.    * facilities of our window.
  168.    */
  169.   if (!(GfxBase = (GFXBASE *)
  170.     OpenLibrary ("graphics.library", GFX_VERSION)))
  171.     panic (CASE_GRAPHICS);
  172.   else
  173.     OpenBits |= CASE_GRAPHICS;
  174.  
  175.   if (!(LayersBase = OpenLibrary ("layers.library", LAYERS_VERSION)))
  176.     panic (CASE_LAYERS);
  177.   else
  178.     OpenBits |= CASE_LAYERS;
  179. }
  180.  
  181.  
  182. /* SetupEnvironment : initializes the screen and window that 
  183.  * will be used. Assumes that all libraries have already been opened
  184.  */
  185.  
  186. void
  187. SetupEnvironment ()
  188. {
  189.  
  190.   if (!Init_MenuPackage ())
  191.     panic (CASE_MENU_PACKAGE);
  192.  
  193.   Init_PointMenu ();
  194.   Init_CurveMenu ();
  195.  
  196.   /* Open a customized Screen */
  197.  
  198.   if (!(Screen = (SCREEN *) OpenScreen (&ScreenInfo)))
  199.     panic (CASE_SCREEN);
  200.   else
  201.     OpenBits |= CASE_SCREEN;
  202.  
  203.   WindowInfo.Screen = Screen;
  204.  
  205.   /* Open A window in the new Window */
  206.  
  207.   if (!(Window = (WINDOW *) OpenWindow (&WindowInfo)))
  208.     panic (CASE_WINDOW);
  209.   else
  210.     OpenBits |= CASE_WINDOW;
  211.  
  212.   /* set up window colors */
  213.  
  214.   LoadRGB4 (ViewPortAddress (Window), (UWORD *) & ColorTable, COLORCOUNT);
  215.  
  216.   SetRast (Window->RPort, ERASE);    /* wipe out everything -- no title bar */
  217. }
  218.  
  219.  
  220. /* panic: Prints out an error message about what went wrong during the
  221.  * setup/initialization phase of this program and ends the program
  222.  * gracefully by calling close_things before exiting with an error code
  223.  */
  224.  
  225. void
  226. panic (error_code)
  227.      int error_code;
  228. {
  229.   switch (error_code)
  230.     {
  231.     case CASE_LAYERS:
  232.       fprintf (stderr, "splines: couldn't open layers library [%d]\n",
  233.            LAYERS_VERSION);
  234.       break;
  235.     case CASE_POPUP_MENU:
  236.       fprintf (stderr, "splines: couldn't allocate popup menu\n");
  237.       break;
  238.     case CASE_MENU_PACKAGE:
  239.       fprintf (stderr, "splines: couldn't initialize menu package\n");
  240.       break;
  241.     case CASE_INTUITION:
  242.       fprintf (stderr, "splines: couldn't find Intuition Library [%d]\n",
  243.            INTUITION_VERSION);
  244.       break;
  245.     case CASE_GRAPHICS:
  246.       fprintf (stderr, "splines: couldn't find Graphics Library [%d]\n",
  247.            GFX_VERSION);
  248.       break;
  249.     case CASE_SCREEN:
  250.       fprintf (stderr, "splines: couldn't open Custom Screen.\n");
  251.       break;
  252.     case CASE_WINDOW:
  253.       fprintf (stderr, "splines: couldn't open Window.\n");
  254.       break;
  255.     }
  256.  
  257.   close_things ();
  258.   exit (error_code);
  259. }
  260.  
  261.  
  262.  
  263.  
  264. /* close_things : closes the Libraries that have been opened and
  265.  * frees up whatever memory has been used.
  266.  */
  267. void
  268. close_things ()
  269. {
  270.   Dispose_PopUp (&PointMenu);
  271.   Dispose_PopUp (&CurveMenu);
  272.   Close_MenuPackage ();
  273.   if (OpenBits & CASE_WINDOW)
  274.     CloseWindow (Window);
  275.   if (OpenBits & CASE_SCREEN)
  276.     CloseScreen (Screen);
  277.   if (OpenBits & CASE_LAYERS)
  278.     CloseLibrary (LayersBase);
  279.   if (OpenBits & CASE_GRAPHICS)
  280.     CloseLibrary ((LIBRARY *) GfxBase);
  281.   if (OpenBits & CASE_INTUITION)
  282.     CloseLibrary ((LIBRARY *) IntuitionBase);
  283. }
  284. /*--*/
  285.